package com.yhsimon.reflection;

import java.lang.annotation.*;
import java.lang.reflect.Field;

public class Test10 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class c1 = Class.forName("com.yhsimon.reflection.Admin");

        //通过反射获得注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }

        //获得注解得value的值
        TableYH tableYH = (TableYH)c1.getAnnotation(TableYH.class);
        System.out.println(tableYH.value());
        System.out.println("=========================");

        //获得类指定成员变量的注解的值
//        Field f = c1.getDeclaredField("name");
        Field f = c1.getDeclaredField("age");
        FieldYH annotation = f.getAnnotation(FieldYH.class);
        System.out.println(annotation.columnName());
        System.out.println(annotation.type());
        System.out.println(annotation.len());

    }
}

@TableYH("db_admin")
class Admin{

    @FieldYH(columnName = "db_id",type = "int",len = 8)
    private int id;
    @FieldYH(columnName = "db_age",type = "int",len = 8)
    private int age;
    @FieldYH(columnName = "db_name",type = "varchar",len=10)
    private String name;

    public Admin(){

    }

    public Admin(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Admin{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableYH{
    String value();
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldYH{
    String columnName();
    String type();
    int len();
}